/*
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Library General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
*/

#include "configurationmodel.h"
#include "constantes.h"

#include <QSettings>
#include <QFile>

ConfigurationManager::ConfigurationManager(QString appName)
{
	m_appName = appName;
	
	defaultFontColor = QColor(Qt::black);
	editorColor = QColor(Qt::white);
	treeColor = QColor(Qt::white);
	
	readSettings();
}

ConfigurationManager::~ConfigurationManager()
{
	writeSettings();
}

void ConfigurationManager::readSettings()
{
	QSettings settings(QSettings::IniFormat, QSettings::UserScope, m_appName, m_appName, 0);

    settings.beginGroup("Configuration");

	// If config file does not exists, it's version is considered as current.
	if (QFile::exists(settings.fileName()))
		stFileVersion = settings.value("FileVersion", QString("Unknown")).toString();
	else
		stFileVersion = Constantes::APP_VERSION;

    defaultFont = settings.value("DefaultFont", QString("Arial")).toString();
    defaultFontSize = settings.value("DefaultFontSize", QString("9")).toString();
    defaultFontColor = settings.value("DefaultFontColor", QColor(Qt::black)).value<QColor>();
    editorColor = settings.value("EditorColor", QColor(Qt::white)).value<QColor>();
    treeColor = settings.value("TreeColor", QColor(Qt::white)).value<QColor>();
    useTrayIcon = settings.value("UseTrayIcon", QVariant(false).toString()).toBool();
    closeToTray = settings.value("CloseToTray", QVariant(false).toString()).toBool();
    showMessageOnCloseToTray = settings.value("ShowMessageOnCloseToTray", QVariant(true).toString()).toBool();
    openLastFile = settings.value("OpenLastFile", QVariant(false).toString()).toBool();
    lastFile = settings.value("LastFile", QString("")).toString();
    dateFormat = settings.value("DateFormat", QString(Constantes::DEFAULT_DATE_FORMAT)).toString();
    timeFormat = settings.value("TimeFormat", QString(Constantes::DEFAULT_TIME_FORMAT)).toString();
    copyImageToUserDir = settings.value("CopyImageToUserDir", QVariant(true).toString()).toBool();
    settings.endGroup();
    
    settings.beginGroup("Search");
    searchWholeWord = settings.value("WholeWord", QVariant(false).toString()).toBool();
    searchCaseSensitive = settings.value("CaseSensitive", QVariant(false).toString()).toBool();
    searchInPages = settings.value("SearchInPages", QVariant(true).toString()).toBool();
    searchInCategories = settings.value("SearchInCategories", QVariant(true).toString()).toBool();
    searchInTags = settings.value("SearchInTags", QVariant(true).toString()).toBool();
    settings.endGroup();

	// In case of incorrect file version, reset some settings.
	if (!isFileVersionCorrect())
		partiallyResetSettings();
}

void ConfigurationManager::writeSettings()
{
	QSettings settings(QSettings::IniFormat, QSettings::UserScope, m_appName, m_appName, 0);

    settings.beginGroup("Configuration");
	settings.setValue("FileVersion", Constantes::APP_VERSION);
    settings.setValue("DefaultFont", defaultFont);
    settings.setValue("DefaultFontSize", defaultFontSize);
    settings.setValue("DefaultFontColor", defaultFontColor);
    settings.setValue("EditorColor", editorColor);
    settings.setValue("TreeColor", treeColor);
    settings.setValue("UseTrayIcon", useTrayIcon);
    settings.setValue("CloseToTray", closeToTray);
    settings.setValue("ShowMessageOnCloseToTray", showMessageOnCloseToTray);
    settings.setValue("OpenLastFile", openLastFile);
    settings.setValue("LastFile", lastFile);
    settings.setValue("DateFormat", dateFormat);
    settings.setValue("TimeFormat", timeFormat);
    settings.setValue("CopyImageToUserDir", copyImageToUserDir);
    settings.endGroup();
    
    settings.beginGroup("Search");
    settings.setValue("WholeWord", searchWholeWord);
    settings.setValue("CaseSensitive", searchCaseSensitive);
    settings.setValue("SearchInPages", searchInPages);
    settings.setValue("SearchInCategories", searchInCategories);
    settings.setValue("SearchInTags", searchInTags);
    settings.endGroup();
}

QString ConfigurationManager::getFullDateFormat()
{
	return dateFormat + " " + timeFormat;
}

bool ConfigurationManager::isFileVersionCorrect()
{
	return (stFileVersion == Constantes::APP_VERSION);
}

/*
	Reset sensible settings to default, in case of settings file from another version.
*/
void ConfigurationManager::partiallyResetSettings()
{
	dateFormat = Constantes::DEFAULT_DATE_FORMAT;
}

QString ConfigurationManager::getResetedSettingsList()
{
	return "    - " + tr("Date format:") + " " + Constantes::DEFAULT_DATE_FORMAT;
}
